import request from '@/utils/request'
import { ApiResponse } from '@/types/api'

// 角色权限相关接口
export const role = {
  // 获取角色权限列表
  getRoleList: (params?: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/home/role/list',
      method: 'get',
      params,
    })
  },

  // 查看单个角色权限详情
  getRoleDetail: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/home/role/${id}`,
      method: 'get',
    })
  },

  // 创建角色权限
  addRole: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/home/role',
      method: 'post',
      data,
    })
  },

  // 更新角色权限
  updateRole: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/home/role/${data.id}`,
      method: 'put',
      data,
    })
  },

  // 删除角色权限
  deleteRole: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/home/role/${id}`,
      method: 'delete',
    })
  },

  // 获取权限节点树
  getPermissionTree: (): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/home/rule/rule_list',
      method: 'get',
    })
  },

  // 获取启用的角色列表
  getActiveRoleList: (): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/home/role/active-list',
      method: 'get',
    })
  },

  // 批量更新角色状态
  batchUpdateRoleStatus: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/home/role/batch-status',
      method: 'post',
      data,
    })
  },

  // 复制角色权限
  copyRole: (id: number, data: any): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/home/role/${id}/copy`,
      method: 'post',
      data,
    })
  },
}

// 向后兼容方法
export const getRoleList = role.getRoleList
export const getRoleDetail = role.getRoleDetail
export const saveRole = (data: any): Promise<ApiResponse<any>> => {
  // 根据是否有id判断是新增还是编辑
  if (data.id && data.id > 0) {
    return role.updateRole(data)
  } else {
    return role.addRole(data)
  }
}
export const deleteRole = role.deleteRole
export const getPermissionTree = role.getPermissionTree
export const getActiveRoleList = role.getActiveRoleList
export const batchUpdateRoleStatus = role.batchUpdateRoleStatus

// 向后兼容的旧方法
export const roleList = role.getRoleList
export const roleView = role.getRoleDetail
export const roleAdd = role.addRole
export const roleEdit = role.updateRole
export const roleDel = role.deleteRole
export const roleSet = role.getActiveRoleList
